home *** CD-ROM | disk | FTP | other *** search
- Path: port4.lightlink.com!user
- From: doug@lightlink.com (Doug Wyatt)
- Newsgroups: comp.lang.c++
- Subject: STL: tiny vectors
- Date: Sat, 13 Apr 1996 22:42:38 -0400
- Organization: none
- Message-ID: <doug-1304962242380001@port4.lightlink.com>
- NNTP-Posting-Host: port4.lightlink.com
-
- I'm relatively new to the STL. I'm experimenting with widespread use of
- the vector<T> class and have run into a problem.
-
- Many of my vectors are rather small, 2-10 small elements. The
- implementation of allocator (HP via Metrowerks CodeWarrior) implements
- memory in chunks of no less than 4096 bytes. When I have many arrays
- under 100 bytes, this adds up to a lot of wasted memory.
-
- I started writing code to make my algorithms multi-pass so I can know in
- advance how large a vector is to be, and call reserve(), but it seems this
- really shouldn't be necessary.
-
- Another thing I've toyed with is using my own allocator which uses
- malloc() and free() instead of the global operators new and delete. That
- way I can use realloc() to shrink a vector. I suppose this will work but
- it occurs to me that if I allocate 3 vectors, put a few elements into
- each of them, then shrink them all, those three memory blocks will still
- be 4K apart, without the possibility of allocating another 4K vector in
- the memory between them. So ultimately memory gets fragmented this way
- anyhow.
-
- I have a book that claims the default allocator should be using a page
- size of 512, so perhaps making my own allocator use 512 would be a simple
- enough solution, though for many -- but not all -- of my vectors this
- would still result in the array being 4 times larger than necessary.
-
- Should I be using another container besides vector? I thought about using
- a list but I'd like to be able to retain fast random access into my
- containers.
-
- Have I overlooked an STL feature which addresses this issue?
-
- Thanks in advance for any suggestions.
-
- Doug
-
- ---
- Doug Wyatt music software
- doug@lightlink.com
- http://www.lightlink.com/doug
- "It is a miracle that curiosity survives formal education."
- - Albert Einstein
-